package com.project.petLossSystem.service.impl;

import com.project.petLossSystem.common.Constants;
import com.project.petLossSystem.common.ServiceResultEnum;
import com.project.petLossSystem.mapper.AddressCategoryMapper;
import com.project.petLossSystem.po.AddressCategory;
import com.project.petLossSystem.po.PetCategory;
import com.project.petLossSystem.service.AddressCategoryService;
import com.project.petLossSystem.util.MyBeanUtil;
import com.project.petLossSystem.util.PageQueryUtil;
import com.project.petLossSystem.util.PageResult;
import com.project.petLossSystem.vo.addressCategory.FirstAddressCategoryVO;
import com.project.petLossSystem.vo.addressCategory.SecondAddressCategoryVO;
import com.project.petLossSystem.vo.addressCategory.ThirdAddressCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class AddressCategoryServiceImpl implements AddressCategoryService {
    @Autowired
    private AddressCategoryMapper addressCategoryMapper;

    @Override
    public PageResult getAddressPage(PageQueryUtil pageQueryUtil) {
        List<AddressCategory> addressCategories = addressCategoryMapper.addressCategoryList(pageQueryUtil);
        int total = addressCategoryMapper.addressCount(pageQueryUtil);
        return new PageResult(total, pageQueryUtil.getRows(), pageQueryUtil.getPage(), addressCategories);
    }

    @Override
    public String insertAddresses(Integer parentId, Byte addressLevel, String[] addressNameArray) {
        // 判断父地址是否被删除
        if (parentId != 0) {
            AddressCategory parentAddress = addressCategoryMapper.getAddressById(parentId);
            if (parentAddress.getIsDeleted() == 1) {
                return ServiceResultEnum.PARENT_ADDRESS_DELETED.getResult();
            }
        }
        List<String> addressNames = new ArrayList<>(); // 存储可插入的
        for (String addressName : addressNameArray) {
            if (StringUtils.hasText(addressName)) {
                // 判断是否已存在同级的同名地址
                AddressCategory tempAddress = addressCategoryMapper.getAddressByParentIdAndName(parentId, addressName);
                if (tempAddress == null) {
                    addressNames.add(addressName);
                }
            }
        }
        if (!addressNames.isEmpty() && addressCategoryMapper.insertAddresses(parentId, addressLevel, addressNames) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateAddress(AddressCategory addressCategory) {
        // 先判断这个地址是否存在
        AddressCategory tempAddress = addressCategoryMapper.getAddressById(addressCategory.getAddressId());
        if (tempAddress == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        // 判断名称是否与其它重复，并且不是同一个
        AddressCategory existAddress = addressCategoryMapper.getAddressByParentIdAndName(addressCategory.getParentId(), addressCategory.getAddressName());
        if (existAddress != null && !existAddress.getAddressId().equals(addressCategory.getAddressId())) {
            return ServiceResultEnum.SAME_ADDRESS_EXIST.getResult();
        }
        // 更新
        if (addressCategoryMapper.updateAddress(addressCategory) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        } else return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public AddressCategory getAddressById(Integer addressId) {
        return addressCategoryMapper.getAddressById(addressId);
    }

    @Override
    public List<Integer> getIdListByParentIds(List<Integer> addressIds) {
        return addressCategoryMapper.getIdListByParentIds(addressIds);
    }

    @Override
    public boolean updateDeleted(List<Integer> addressIds) {
        return addressCategoryMapper.updateDeletedByIds(addressIds) > 0;
    }

    /**
     * 先判断空，后两个判断非空，是因为一级分类必须要有，后面的无所谓
     */
    @Override
    public List<FirstAddressCategoryVO> getAddressesForSelect() {
        // 获取一级地址
        List<AddressCategory> addresses = addressCategoryMapper.getAddressesByParentId(Constants.TOP_PARENT_ID);
        if (CollectionUtils.isEmpty(addresses)) {
            return null;
        }
        List<FirstAddressCategoryVO> firstAddresses = MyBeanUtil.copyList(addresses, FirstAddressCategoryVO.class);
        // 取第一个获取二级子地址
        FirstAddressCategoryVO firstAddress = firstAddresses.get(0);
        addresses = addressCategoryMapper.getAddressesByParentId(firstAddress.getAddressId());
        if (!CollectionUtils.isEmpty(addresses)) {
            List<SecondAddressCategoryVO> firstAddressChildren = MyBeanUtil.copyList(addresses, SecondAddressCategoryVO.class);
            firstAddress.setSecondAddressCategories(firstAddressChildren); // 存入二级
            // 取第一个获取三级子地址
            SecondAddressCategoryVO secondAddress = firstAddressChildren.get(0);
            addresses = addressCategoryMapper.getAddressesByParentId(secondAddress.getAddressId());
            if (!CollectionUtils.isEmpty(addresses)) {
                List<ThirdAddressCategoryVO> secondAddressChildren = MyBeanUtil.copyList(addresses, ThirdAddressCategoryVO.class);
                secondAddress.setThirdAddressCategories(secondAddressChildren); // 存入三级
            }
        }
        return firstAddresses;
    }

    @Override
    public List<SecondAddressCategoryVO> getSecondAddresses(Integer addressId) {
        // 获取二级地址
        List<AddressCategory> addresses = addressCategoryMapper.getAddressesByParentId(addressId);
        if (CollectionUtils.isEmpty(addresses)) {
            return null;
        }
        List<SecondAddressCategoryVO> secondAddresses = MyBeanUtil.copyList(addresses, SecondAddressCategoryVO.class);
        // 取第一个获取三级子地址
        SecondAddressCategoryVO secondAddress = secondAddresses.get(0);
        addresses = addressCategoryMapper.getAddressesByParentId(secondAddress.getAddressId());
        if (!CollectionUtils.isEmpty(addresses)) {
            List<ThirdAddressCategoryVO> secondAddressChildren = MyBeanUtil.copyList(addresses, ThirdAddressCategoryVO.class);
            secondAddress.setThirdAddressCategories(secondAddressChildren); // 存入三级
        }
        return secondAddresses;
    }

    @Override
    public List<ThirdAddressCategoryVO> getThirdAddresses(Integer addressId) {
        // 获取三级地址
        List<AddressCategory> addresses = addressCategoryMapper.getAddressesByParentId(addressId);
        if (CollectionUtils.isEmpty(addresses)) {
            return null;
        }
        return MyBeanUtil.copyList(addresses, ThirdAddressCategoryVO.class);
    }

}
